<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加采购入库单' : '编辑采购入库单'" :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="鱼类名称：" prop="fishTypeId">
              <el-select v-model="ruleForm.fishTypeId" placeholder="请选择鱼类名称" clearable>
                <el-option v-for="item in fishTypeArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="鱼类年份：" prop="fishTypeYear">
              <el-input v-model="ruleForm.fishTypeYear" placeholder="请输入鱼类年份" clearable maxlength="50"
                show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量：" prop="fishNum">
              <el-input-number v-model="ruleForm.fishNum" placeholder="请输入数量" :precision="0" :step="1" :max="9999"
                :min="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卵径：" prop="fishEggSize">
              <el-input-number v-model="ruleForm.fishEggSize" placeholder="请输入卵径" :precision="2" :step="1" :max="9999"
                :min="0"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="初重：" prop="fishInitWeight">
              <el-input-number v-model="ruleForm.fishInitWeight" placeholder="请输入初重" :precision="2" :step="1" :max="9999"
                :min="0"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当重：" prop="fishNowWeight">
              <el-input-number v-model="ruleForm.fishNowWeight" placeholder="请输入当重" :precision="2" :step="1" :max="9999"
                :min="0"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="均重：" prop="fishAverageWeight">
              <el-input-number v-model="ruleForm.fishAverageWeight" placeholder="请输入均重" :precision="2" :step="1"
                :max="9999" :min="0"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getObjFishpondDetail, saveObjFishpondDetail } from "@/api/fishpond";
import { getSysFishTypes } from "@/api/system";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    fishpondId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        fishpondId: "",
        fishTypeId: "",
        fishTypeYear: "",
        fishNum: "",
        fishEggSize: "",
        fishInitWeight: "",
        fishNowWeight: "",
        fishAverageWeight: "",
      },
      rules: {
        // 表单规则
        fishTypeId: [{ required: true, message: "请选择鱼类名称", trigger: "change" }],
        fishTypeYear: [{ required: true, message: "请输入鱼类年份", trigger: "blur" }],
        fishNum: [{ required: true, message: "请输入数量", trigger: "blur" }],
        fishEggSize: [{ required: true, message: "请输入卵径", trigger: "blur" }],
        fishInitWeight: [{ required: true, message: "请输入初重", trigger: "blur" }],
        fishNowWeight: [{ required: true, message: "请输入当重", trigger: "blur" }],
        fishAverageWeight: [{ required: true, message: "请输入当重", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      fishTypeArray: [], // 鱼类集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        this.ruleForm.fishpondId = this.fishpondId;
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.userType = Number(window.localStorage.getItem("userType"));
        this.getSysFishTypes();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {

      let a = {
        id: this.ruleForm.id
      }
     
      // getObjFishpondDetail(a).then((res) => {
      //   if (res.code == 200) {
      //     this.ruleForm = res.data;
      //     this.getSysFishTypes();
      //   }
      // });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveObjFishpondDetail(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    getSysFishTypes() {
      getSysFishTypes().then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}

.address_icon {
  width: 30px;
  height: 30px;
  margin-left: 225px;
  margin-top: -35px;
  display: block;
  cursor: pointer;
}
</style>
